<?php

defined('SYSPATH') or die('No direct script access.');

class Utorder
{

    private static $instances;
    private $data;

    public static function & instances($id = 0)
    {

        if (!isset(self::$instances[$id]))
        {
            $class = __CLASS__;
            self::$instances[$id] = new $class($id);
        }
        return self::$instances[$id];
    }

    public function __construct($id)
    {

        $this->data = NULL;
        $this->_load($id);
    }

    public function _load($id)
    {

        if (!$id)
            return FALSE;

        $result = ORM::factory('type')
                ->where('id_type', '=', $id)
                ->find()
                ->as_array();

        $this->data = $result ? $result : array();
    }

    public function get($key = NULL)
    {

        if (is_null($key))
        {
            return $this->data;
        }
        else
        {
            return isset($this->data[$key]) ? $this->data[$key] : NULL;
        }
    }

    public function set($data)
    {

        $type = ORM::factory('type');
        $type->values($data);
        if ($type->check())
        {
            $type->save();
            if ($type->saved())
            {
                return true;
            }
        }

        return false;
    }

    public function delete()
    {
        $type = ORM::factory('utorder');
        if ($type->loaded())
        {
            return $type->delete();
        }
        else
        {
            return FALSE;
        }
    }

    public function get_utorders(array $admintype = array())
    {
        $utorders = DB::select()->from('utorders')
                        ->where('uto_utid', 'in', $admintype)
                        ->and_where('uto_type', '=', 0)
                        ->execute()->as_array();
       
        if(count($utorders)>0){
             return $utorders;
        }
            
       
    }

}

